<template>
    <div>
        <h2>房型添加</h2>
        <table class="table table-bordered">
            <tr>
                <td align="right">房型名称</td>
                <td><input type="text" v-model="state.htName" /></td>
            </tr>
            <tr>
                <td align="right">门市价</td>
                <td><input type="text" v-model="state.money" /></td>
            </tr>
            <tr>
                <td align="right">房间数量</td>
                <td><input type="text" v-model="state.number" /></td>
            </tr>
            <tr>
                <td align="right">状态</td>
                <td>
                    <input type="radio" name="r1" value="1" v-model="state.htState" />启用
                    <input type="radio" name="r1" value="0" v-model="state.htState" />停用
                </td>
            </tr>
            <tr>
                <td align="right">房号</td>
                <td><input type="text" v-model="state.houseNumber" /></td>
            </tr>
            <tr>
                <td align="right"></td>
                <td><input type="button" value="增加" @click="add" /></td>
            </tr>
        </table>
    </div>
</template>

<script setup lang="ts">
import { reactive, toRefs } from 'vue';
import axios from 'axios';
import { useRouter, useRoute } from 'vue-router';
let router = useRouter();
let data = reactive({
    state: {
        htName: "",
        money: "",
        number: "",
        houseNumber: "",
        htState: 1,
    }
})
let { state } = toRefs(data)
const add = () => {
    if(state.value.htName==""){
        alert("房型名称不能为空")
        return
    }
    if(state.value.money==""){
        alert("门市价不能为空")
        return
    }
    if(isNaN(Number(state.value.money))==true){
        alert("门市价只能为数字")
        return
    }
    if(state.value.number==""){
        alert("房间数量不能为空")
        return
    }
    if(isNaN(Number(state.value.number))==true){
        alert("房间数量只能为数字")
        return
    }
    if(state.value.houseNumber==""){
        alert("房号不能为空")
        return
    }
    let obj = {
        htName: state.value.htName,
        money: state.value.money,
        number: state.value.number,
        houseNumber: state.value.houseNumber,
        htState: state.value.htState,
    }
    axios({
        url: '/api/HouseType/AddHouseType',
        method: 'post',
        data: obj
    })
        .then(res => {
            console.log(res)
            if (res.data.data == 1) {
                alert("增加成功")
                router.push("/list")
            }
            if (res.data.data == -1) {
                alert("房型名称不能重复")
                return
            }

        })
        .catch(err => {
            console.log(err)
        })
}


</script>

<style scoped></style>